
# plot_cov_timeseries.py (robust)
import csv, sys
import matplotlib.pyplot as plt

def load_csv(path):
    rows = []
    with open(path, "r", encoding="utf-8") as f:
        r = csv.DictReader(f)
        for row in r:
            rows.append({
                "t_sec": float(row.get("t_sec", 0.0)),
                "line_pct": float(row.get("line_pct", 0.0)),
                "branch_pct": float(row.get("branch_pct", 0.0)),
            })
    return rows

def plot_one(ax, rows, label_prefix):
    t = [r["t_sec"] for r in rows]
    lp = [r["line_pct"] for r in rows]
    bp = [r["branch_pct"] for r in rows]
    ax.plot(t, lp, label=f"{label_prefix} line%")
    # Only plot branch if it looks non-trivial
    if any(x > 0.0 for x in bp):
        ax.plot(t, bp, label=f"{label_prefix} branch%")

def main():
    if len(sys.argv) < 2:
        print("Usage: python plot_cov_timeseries.py server_cov.csv [master_cov.csv]")
        sys.exit(1)
    paths = sys.argv[1:]
    fig, ax = plt.subplots(figsize=(8,5))
    for i, p in enumerate(paths):
        rows = load_csv(p)
        label = "server" if i == 0 else ("master" if i == 1 else f"run{i+1}")
        plot_one(ax, rows, label)
    ax.set_xlabel("Time (s)")
    ax.set_ylabel("Coverage (%)")
    ax.set_title("Coverage Efficiency Curve")
    ax.legend()
    ax.grid(True, linestyle="--", linewidth=0.5, alpha=0.5)
    fig.tight_layout()
    fig.savefig("./coverage_efficiency.png", dpi=150)
    print("Saved figure to ./coverage_efficiency.png")

if __name__ == "__main__":
    main()
